package com.xiupeilian.car.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.mail.iap.Response;
import com.xiupeilian.car.model.User;
import com.xiupeilian.car.service.UserService;
import com.xiupeilian.car.vo.LoginVo;

@Controller
@RequestMapping("/login")
public class LoginAction {
	@Resource
	private UserService userService;
	@RequestMapping("/tologin.shtml")
	public String toLogin(){
		return "commons/login";
	}
	@RequestMapping("/login.shtml")
	public void login(HttpServletResponse response,HttpSession session,LoginVo vo) throws Exception{
		String code=(String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if(vo.getValidateCode().toUpperCase().equals(code.toUpperCase())){
			User user=userService.findUserByLoginNameAndPassword(vo);
			if(user==null){
				response.getWriter().write("2");
			}else{
				session.setAttribute("user", user);
				response.getWriter().write("3");
			}
			
			
		}else{
			response.getWriter().write("1");
		}
		
	}
	
	@RequestMapping("/logout.shtml")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:tologin.shtml";
	}
	
	@RequestMapping("/toGetPassword.shtml")
	public String toGetPassword(){
		return "commons/getPassword";
	}
	
	@RequestMapping("getPassword.shtml")
	public void getPassword(HttpServletResponse response,User user){
		User checkUser=userService.findUserByLoginNameAndEmail(user);
	}
	
	
	
	
	
}
